package com.dexilaonian.activity;


import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.dexilaonian.R;
import com.dexilaonian.adpter.ServiceDetailAdaprer;
import com.dexilaonian.ui.BaseActivity;
import com.dexilaonian.utils.Constant;
import com.dexilaonian.utils.Okhttp;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;


public class ServiceDetailActivity extends BaseActivity implements View.OnClickListener {
	@Bind(R.id.tv_content_service)
	TextView tvContentService;
	@Bind(R.id.iv_content_service)
	View     ivContentService;
	@Bind(R.id.tv_tatoo)
	TextView tvTatoo;
	@Bind(R.id.iv_tatoo)
	View  ivTatoo;
	@Bind(R.id.tv_order_imfor)
	TextView tvOrderImfor;
	@Bind(R.id.iv_order_imfor)
	View     ivOrderImfor;
	@Bind(R.id.piantan)
	TextView mPiantan;
	@Bind(R.id.piantan_price)
	TextView mPiantanPrice;
	private List<View> service_list = new ArrayList<>();
	@Bind(R.id.back)
	ImageView back;
	@Bind(R.id.hei)
	ImageView ivKangyangshi;
	@Bind(R.id.tv_kanyangshi)
	TextView  tvKanyangshi;
	@Bind(R.id.iv_kangyang_zhuren)
	ImageView ivKangyangZhuren;
	@Bind(R.id.tv_zhuren)
	TextView  tvZhuren;
	@Bind(R.id.iv_teping)
	ImageView ivTeping;
	@Bind(R.id.tv_teping)
	TextView  tvTeping;
	@Bind(R.id.kangyang_ViewPager)
	ViewPager kangyangViewPager;
	@Bind(R.id.btn_kangyang_door)
	Button    btnKangyangDoor;
	private ServiceDetailAdaprer serviceDetailAdapter;
	private ServiceDetailAdaprer serviceDetail;
	private String               mIdd;
	private TextView             mTatooView;
	private TextView             mOrderView;
	private TextView             mContentView;
	private View                 mContenterVice;
	private View                 mTatoo;
	private View                 mService_order;
	private String projectid;
	private String               mContent_time;
	private String               mNotice;
	private String               mContent_method;
	private String               detail_url;
	private int                  mPrice;
	private int mPrice2;
	private int mPrice3;

	@Override
	public int getLayoutID() {
		return R.layout.activity_service_detail;
	}

	@Override
	public void initView() {
		detail_url = Constant.base_url + "/service/getDetailOfService";
	}

	@Override
	protected void initData() {

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// TODO: add setContentView(...) invocation
		ButterKnife.bind(this);
		Intent intent = getIntent();
		Bundle data   = intent.getExtras();
		mIdd = String.valueOf(data.getInt("IDD"));
		init();
		initGetDetail();
	}

	private void initGetDetail() {
		Map map = new HashMap<>();
		map.put("serviceid", mIdd);
		Okhttp.postString(false, detail_url, map, new Okhttp.CallBac() {
			@Override
			public void onError(Call call, Exception e, String state, int id) {
			}
			@Override
			public void onResponse(String response, int id) {
				try {
					JSONObject object = new JSONObject(response);
					int        status = Integer.parseInt(object.optString("status"));
					if (status == 34 || status == 16) {
						startActivity(new Intent(ServiceDetailActivity.this, LoginActivity.class));
						return;
					}
					if (status == 0) {
						String data  = object.optString("data");
						JSONObject jsonObject  = new JSONObject(data);
						String content_people = jsonObject.optString("content_people");
						mPrice = jsonObject.optInt("price");
						mPrice2 = jsonObject.optInt("price2");
						mPrice3 = jsonObject.optInt("price3");
						Log.e("TTT", mPrice +"");
					/*	mPrice2 = Integer.parseInt(jsonObject.optString("price2"));
						mPrice3 = Integer.parseInt(jsonObject.optString("price3"));*/
						projectid = jsonObject.optString("id");
						Log.e("TTTTprojectid", projectid);
						mContent_method = jsonObject.optString("content_method");
						mContent_time = jsonObject.optString("content_time");
						String     suggest   = jsonObject.optString("suggest");
						String     suggest2   = suggest.replace("\\n", "\n");
						String     name     = jsonObject.optString("name");
						mNotice = jsonObject.optString("notice");
						mContentView = (TextView) mContenterVice.findViewById(R.id.tv_content_view);
						mTatooView = (TextView) mTatoo.findViewById(R.id.tv_tatoo_view);

						mOrderView = (TextView) mService_order.findViewById(R.id.tv_order_view);
						if (!TextUtils.isEmpty(name)) {
							mPiantan.setText(name);
						}

						mPiantanPrice.setText("￥"+ mPrice);
						if (!TextUtils.isEmpty(content_people)) {
							mContentView.setText(content_people);
						}
						if (!TextUtils.isEmpty(mNotice)) {
							mTatooView.setText(mNotice);
						}
						if (!TextUtils.isEmpty(suggest2)) {
							mOrderView.setText(suggest2);
						}
						service_list.add(mContenterVice);
						service_list.add(mTatoo);
						service_list.add(mService_order);
						serviceDetail = new ServiceDetailAdaprer(ServiceDetailActivity.this, service_list);
						kangyangViewPager.setAdapter(serviceDetail);
						serviceDetail.notifyDataSetChanged();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onNoNetwork(String state) {

			}
		});
	}


	private void init() {
		mContenterVice = View.inflate(ServiceDetailActivity.this, R.layout.fragment_contentervice,
																			  null);

		mTatoo = View.inflate(ServiceDetailActivity.this,
				R.layout.fragment_tatoo,
				null);

		mService_order = View.inflate(ServiceDetailActivity.this,
									  R.layout.fragment_order,
									  null);

		tvContentService.setOnClickListener(this);
		tvOrderImfor.setOnClickListener(this);
		tvTatoo.setOnClickListener(this);
		ivKangyangshi.setOnClickListener(this);
		ivTeping.setOnClickListener(this);
		ivKangyangZhuren.setOnClickListener(this);

		kangyangViewPager.setCurrentItem(0);
		kangyangViewPager.setOffscreenPageLimit(3);
		kangyangViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			@Override
			public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
			{

			}

			@Override
			public void onPageSelected(int position) {
				chanceColorText(position);
			}

			@Override
			public void onPageScrollStateChanged(int state) {

			}
		});
	}

	private void chanceColorText(int position) {
		if (position == 0) {
			//mPiantanPrice.setText(mPrice);
			tvContentService.setTextColor(Color.parseColor("#009a99"));
			tvTatoo.setTextColor(Color.parseColor("#333333"));
			tvOrderImfor.setTextColor(Color.parseColor("#333333"));
			ivContentService.setBackground(getResources().getDrawable(R.drawable.fuwuxiangqingxiahuaxian));
			ivTatoo.setBackground(null);
			ivOrderImfor.setBackground(null);

		} else if (position == 1) {
			tvTatoo.setTextColor(Color.parseColor("#009a99"));
			tvContentService.setTextColor(Color.parseColor("#333333"));
			tvOrderImfor.setTextColor(Color.parseColor("#333333"));
			ivTatoo.setBackground(getResources().getDrawable(R.drawable.fuwuxiangqingxiahuaxian));
			ivContentService.setBackground(null);
			ivOrderImfor.setBackground(null);
		} else if (position == 2) {
			//mPiantanPrice.setText(mPrice3);
			tvOrderImfor.setTextColor(Color.parseColor("#009a99"));
			tvContentService.setTextColor(Color.parseColor("#333333"));
			tvTatoo.setTextColor(Color.parseColor("#333333"));
			ivOrderImfor.setBackground(getResources().getDrawable(R.drawable.fuwuxiangqingxiahuaxian));
			ivContentService.setBackground(null);
			ivTatoo.setBackground(null);
		}
	}

	@OnClick({R.id.back,
			  R.id.hei,
			  R.id.iv_kangyang_zhuren,
			  R.id.iv_teping,
			  R.id.btn_kangyang_door,
			  R.id.tv_content_service,
			  R.id.tv_tatoo,
			  R.id.tv_order_imfor})
	public void onClick(View view) {
		switch (view.getId()) {
			case R.id.btn_kangyang_door:
				Intent intent = new Intent(ServiceDetailActivity.this, ConfirmOrderActivity.class);
				intent.putExtra("projectid", projectid);
				Bundle bundle = new Bundle();
				bundle.putInt("PRICE", mPrice);
				bundle.putInt("customer_id",0);
				intent.putExtras(bundle);
				intent.putExtra("mContent_method", mContent_method);
				intent.putExtra("mNotice", mNotice);
				intent.putExtra("mContent_time", mContent_time);
				startActivity(intent);
				break;
			case R.id.tv_content_service:
				kangyangViewPager.setCurrentItem(0);
				tvContentService.setTextColor(Color.parseColor("#009a99"));
				tvTatoo.setTextColor(Color.parseColor("#333333"));
				tvOrderImfor.setTextColor(Color.parseColor("#333333"));
				ivContentService.setBackground(getResources().getDrawable(R.drawable.fuwuxiangqingxiahuaxian));
				ivTatoo.setBackground(null);
				ivOrderImfor.setBackground(null);
				break;
			case R.id.tv_tatoo:
				kangyangViewPager.setCurrentItem(1);
				tvTatoo.setTextColor(Color.parseColor("#009a99"));
				tvContentService.setTextColor(Color.parseColor("#333333"));
				tvOrderImfor.setTextColor(Color.parseColor("#333333"));
				ivTatoo.setBackground(getResources().getDrawable(R.drawable.fuwuxiangqingxiahuaxian));
				ivContentService.setBackground(null);
				ivOrderImfor.setBackground(null);
				break;
			case R.id.tv_order_imfor:
				kangyangViewPager.setCurrentItem(2);
				tvOrderImfor.setTextColor(Color.parseColor("#009a99"));
				tvContentService.setTextColor(Color.parseColor("#333333"));
				tvTatoo.setTextColor(Color.parseColor("#333333"));
				ivOrderImfor.setBackground(getResources().getDrawable(R.drawable.fuwuxiangqingxiahuaxian));
				ivContentService.setBackground(null);
				ivTatoo.setBackground(null);
				break;
			case R.id.back:
				finish();
				break;
			case R.id.hei:
				mPiantanPrice.setText("￥"+mPrice);
				/*if (!TextUtils.isEmpty(mPrice)) {
				}*/
				tvKanyangshi.setTextColor(Color.parseColor("#ffffff"));
				tvZhuren.setTextColor(Color.parseColor("#666666"));
				tvTeping.setTextColor(Color.parseColor("#666666"));
				ivKangyangshi.setImageResource(R.drawable.yixuanzeleixing);
				ivKangyangZhuren.setImageResource(R.drawable.weixuanzeleixing);
				ivTeping.setImageResource(R.drawable.weixuanzeleixing);
				break;
			case R.id.iv_kangyang_zhuren:
				mPiantanPrice.setText("￥"+mPrice2);
			/*	if (!TextUtils.isEmpty(mPrice2)) {
				}*/
				tvZhuren.setTextColor(Color.parseColor("#ffffff"));
				tvKanyangshi.setTextColor(Color.parseColor("#666666"));
				tvTeping.setTextColor(Color.parseColor("#666666"));
				ivKangyangZhuren.setImageResource(R.drawable.yixuanzeleixing);
				ivKangyangshi.setImageResource(R.drawable.weixuanzeleixing);
				ivTeping.setImageResource(R.drawable.weixuanzeleixing);
				break;
			case R.id.iv_teping:
				mPiantanPrice.setText("￥"+mPrice3);
				/*if (!TextUtils.isEmpty(mPrice3)) {
				}*/
				tvTeping.setTextColor(Color.parseColor("#ffffff"));
				tvKanyangshi.setTextColor(Color.parseColor("#666666"));
				tvZhuren.setTextColor(Color.parseColor("#666666"));
				ivTeping.setImageResource(R.drawable.yixuanzeleixing);
				ivKangyangshi.setImageResource(R.drawable.weixuanzeleixing);
				ivKangyangZhuren.setImageResource(R.drawable.weixuanzeleixing);
				break;

		}
	}

}
